package com.gzc.just.play.last.war.sceneserver.battle.model;

import jakarta.persistence.*;
import java.io.Serializable;

/**
 * 战斗用户信息（对应C++的SBattleUserInfo）
 * 存储单个玩家在战斗中的相关信息
 */
@Entity
@Table(name = "battle_user_info")
@IdClass(BattleUserInfoId.class)
public class BattleUserInfo implements Serializable {
    
    // 构造函数
    public BattleUserInfo() {
        // 初始化默认值
        this.heroChoosed = false;
        this.loadComplete = false;
        this.reconnected = false;
        this.offline = false;
        this.isAI = false;
        this.isGM = false;
        this.isObserver = false;
    }
    
    public BattleUserInfo(Long userId, String nickname, Long characterId, Integer characterTypeId) {
        this();
        this.userId = userId;
        this.nickname = nickname;
        this.characterId = characterId;
        this.characterTypeId = characterTypeId;
    }
    private static final long serialVersionUID = 1L;

    // ==================== 主键 ====================
    /** 战斗ID（联合主键的一部分） */
    @Id
    @Column(name = "battle_id")
    private Long battleId;
    
    /** 战斗信息引用 */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "battle_id", referencedColumnName = "battle_id", insertable = false, updatable = false)
    private BattleInfo battleInfo;
    
    // ==================== 用户基本信息 ====================
    /** 玩家用户ID */
    @Id
    private Long userId;
    
    /** 玩家昵称 */
    private String nickname;
    
    /** 玩家选择的角色ID */
    private Long characterId;
    
    /** 角色类型ID */
    private Integer characterTypeId;
    
    // ==================== 战斗状态 ====================
    /** 是否已选择角色 */
    private Boolean heroChoosed;
    
    /** 角色已加载 */
    private Boolean loadComplete;
    
    /** 是否重新连接 */
    private Boolean reconnected;
    
    /** 是否掉线 */
    private Boolean offline;
    
    // ==================== 队伍信息 ====================
    /** 队伍ID (1或2) */
    private Integer teamId;
    
    /** 队伍内位置 */
    private Integer position;
    
    // ==================== 角色信息 ====================
    /** 当前生命值 */
    private Integer currentHealth;
    
    /** 最大生命值 */
    private Integer maxHealth;
    
    /** 攻击力 */
    private Integer attack;
    
    /** 防御力 */
    private Integer defense;
    
    /** 速度 */
    private Integer speed;
    
    /** 技能ID列表 */
    private String skillIds;
    
    // ==================== 战斗结果 ====================
    /** 杀敌数 */
    private Integer killCount;
    
    /** 死亡次数 */
    private Integer deathCount;
    
    /** 获得的金币 */
    private Integer goldGain;
    
    /** 获得的经验 */
    private Integer expGain;
    
    /** 连杀数 */
    private Integer killStreak;
    
    /** 最大连杀 */
    private Integer maxKillStreak;
    
    // ==================== 其他信息 ====================
    /** 是否为AI */
    private Boolean isAI;
    
    /** 是否为GM用户 */
    private Boolean isGM;
    
    /** 是否为观战者 */
    private Boolean isObserver;
    
    /** 是否为获胜者 */
    private Boolean isWinner;
    
    /** 离线时间 */
    private Long offlineTime;
    
    /**
     * 增加杀敌数
     */
    public void addKill() {
        this.killCount = (this.killCount == null ? 0 : this.killCount) + 1;
        updateKillStreak();
    }
    
    /**
     * 增加死亡次数
     */
    public void addDeath() {
        this.deathCount = (this.deathCount == null ? 0 : this.deathCount) + 1;
        this.killStreak = 0;
    }
    
    /**
     * 更新连杀数
     */
    private void updateKillStreak() {
        if (this.killStreak == null) {
            this.killStreak = 0;
        }
        this.killStreak++;
        
        if (this.maxKillStreak == null || this.killStreak > this.maxKillStreak) {
            this.maxKillStreak = this.killStreak;
        }
    }
    
    /**
     * 检查是否活着
     */
    public boolean isAlive() {
        return currentHealth != null && currentHealth > 0;
    }
    
    /**
     * 恢复生命值
     */
    public void recoverHealth(int amount) {
        if (this.currentHealth == null) {
            this.currentHealth = 0;
        }
        this.currentHealth = Math.min(this.currentHealth + amount, this.maxHealth);
    }
    
    /**
     * 受伤
     */
    public void takeDamage(int damage) {
        if (this.currentHealth == null) {
            this.currentHealth = this.maxHealth;
        }
        this.currentHealth = Math.max(0, this.currentHealth - damage);
    }
    
    // ==================== Getter和Setter方法 ====================
    
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public String getNickname() {
        return nickname;
    }
    
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    
    public Long getCharacterId() {
        return characterId;
    }
    
    public void setCharacterId(Long characterId) {
        this.characterId = characterId;
    }
    
    public Integer getCharacterTypeId() {
        return characterTypeId;
    }
    
    public void setCharacterTypeId(Integer characterTypeId) {
        this.characterTypeId = characterTypeId;
    }
    
    public Boolean getHeroChoosed() {
        return heroChoosed;
    }
    
    public void setHeroChoosed(Boolean heroChoosed) {
        this.heroChoosed = heroChoosed;
    }
    
    public Boolean getLoadComplete() {
        return loadComplete;
    }
    
    public void setLoadComplete(Boolean loadComplete) {
        this.loadComplete = loadComplete;
    }
    
    public Boolean getReconnected() {
        return reconnected;
    }
    
    public void setReconnected(Boolean reconnected) {
        this.reconnected = reconnected;
    }
    
    public Boolean getOffline() {
        return offline;
    }
    
    public void setOffline(Boolean offline) {
        this.offline = offline;
    }
    
    public Integer getTeamId() {
        return teamId;
    }
    
    public void setTeamId(Integer teamId) {
        this.teamId = teamId;
    }
    
    public Integer getPosition() {
        return position;
    }
    
    public void setPosition(Integer position) {
        this.position = position;
    }
    
    public Integer getCurrentHealth() {
        return currentHealth;
    }
    
    public void setCurrentHealth(Integer currentHealth) {
        this.currentHealth = currentHealth;
    }
    
    public Integer getMaxHealth() {
        return maxHealth;
    }
    
    public void setMaxHealth(Integer maxHealth) {
        this.maxHealth = maxHealth;
    }
    
    public Integer getAttack() {
        return attack;
    }
    
    public void setAttack(Integer attack) {
        this.attack = attack;
    }
    
    public Integer getDefense() {
        return defense;
    }
    
    public void setDefense(Integer defense) {
        this.defense = defense;
    }
    
    public Integer getSpeed() {
        return speed;
    }
    
    public void setSpeed(Integer speed) {
        this.speed = speed;
    }
    
    public String getSkillIds() {
        return skillIds;
    }
    
    public void setSkillIds(String skillIds) {
        this.skillIds = skillIds;
    }
    
    public Integer getKillCount() {
        return killCount;
    }
    
    public void setKillCount(Integer killCount) {
        this.killCount = killCount;
    }
    
    public Integer getDeathCount() {
        return deathCount;
    }
    
    public void setDeathCount(Integer deathCount) {
        this.deathCount = deathCount;
    }
    
    public Integer getGoldGain() {
        return goldGain;
    }
    
    public void setGoldGain(Integer goldGain) {
        this.goldGain = goldGain;
    }
    
    public Integer getExpGain() {
        return expGain;
    }
    
    public void setExpGain(Integer expGain) {
        this.expGain = expGain;
    }
    
    public Integer getKillStreak() {
        return killStreak;
    }
    
    public void setKillStreak(Integer killStreak) {
        this.killStreak = killStreak;
    }
    
    public Integer getMaxKillStreak() {
        return maxKillStreak;
    }
    
    public void setMaxKillStreak(Integer maxKillStreak) {
        this.maxKillStreak = maxKillStreak;
    }
    
    public Boolean getIsAI() {
        return isAI;
    }
    
    public void setIsAI(Boolean isAI) {
        this.isAI = isAI;
    }
    
    public Boolean getIsGM() {
        return isGM;
    }
    
    public void setIsGM(Boolean isGM) {
        this.isGM = isGM;
    }
    
    public Boolean getIsObserver() {
        return isObserver;
    }
    
    public void setIsObserver(Boolean isObserver) {
        this.isObserver = isObserver;
    }
    
    public Long getOfflineTime() {
        return offlineTime;
    }
    
    public void setOfflineTime(Long offlineTime) {
        this.offlineTime = offlineTime;
    }
    
    public Boolean getIsWinner() {
        return isWinner;
    }
    
    public void setIsWinner(Boolean isWinner) {
        this.isWinner = isWinner;
    }
    
    public BattleInfo getBattleInfo() {
        return battleInfo;
    }
    
    public void setBattleInfo(BattleInfo battleInfo) {
        this.battleInfo = battleInfo;
    }
}
